$(document).ready(function () {
    //时间初始化
    $("div#list div.query form input[name='Start']").val($.getDate(0, -1));
    $("div#list div.query form input[name='End']").val($.getCurrentDate());
    function A(C) {
        var D = $("div#list table.list tbody");
        D.empty();
        D.append('<tr><td colspan="20">数据加载中...</td></tr>');
        $.ajax({
            url: "PayPage",
            dataType: "json",
            data: "page=" + C,
            timeout: timeout,
            success: function (E) {
                update(E);
            }
        }).fail(function (F, E) {
            alert("浏览器超时未响应，请刷新重试");
        });
    }
    var B = {
        count: 1,
        start: 1,
        display: 20,
        border: true,
        border_color: "#DDD",
        text_color: "#3399D5",
        background_color: "#FFF",
        border_hover_color: "#DDD",
        text_hover_color: "#737373",
        background_hover_color: "#F5F5F5",
        images: false,
        mouse: "press",
        onChange: A
    };
    $("div#lpager").paginate(B);
    window.update = function (V) {
        var X = $("div#list table.list tbody");
        var S = V.Items;
        X.empty();
        var P = "</td><td>";
        if (S && S.length > 0) {
            var W = 0,
				O = 0,
				N = 0,
				G = 0;
            for (var I = 0; I < S.length; I++) {
                var J = S[I];
                var D = J.length;
                if (D > 0) {
                    var F = J[0];
                    var Q = '<tr><td rowspan="' + (D + 1) + '">';
                    var E = '</td><td rowspan="' + (D + 1) + '">';
                    var T = '</td><td rowspan="' + D + '">';
                    var U = F.Hours;
                    var C = F.Adjust || 0;
                    var M = F.Pay || 0;
                    X.append(Q + F.CrewName + E + F.CrewType + T + $.toSpace(F.Start) + P + F.ServiceType + P + F.ServiceName + P + F.ShiftName + P + F.Hours + P + F.CustomerName + P + F.Area + P + F.Bed + P + F.Company + P + C + P + M + P + F.Note + "</td></tr>");
                    for (var H = 1; H < J.length; H++) {
                        F = J[H];
                        var K = F.Note || "";
                        U += F.Hours;
                        var L = F.Adjust || 0;
                        C += L;
                        var R = F.Pay || 0;
                        M += R;
                        X.append("<tr><td>" + F.ServiceType + P + F.ServiceName + P + F.ShiftName + P + F.Hours + P + F.CustomerName + P + F.Area + P + F.Bed + P + F.Company + P + L + P + R + P + K + "</td></tr>");
                    }
                    W += U;
                    O += J.length;
                    N += C;
                    G += M;
                    X.append('<tr class="sum"><td>小计</td><td></td><td></td><td></td><td>' + U + P + J.length + "人</td><td>" + P + P + P + C + P + M + "</td><td></td></tr>");
                }
            }
            X.append('<tr class="sum"><td colspan="3">合计</td><td></td><td></td><td></td><td>' + W + P + O + "人</td><td>" + P + P + P + N + P + G + "</td><td></td></tr>");
        } else {
            X.append('<tr><td colspan="20">没有相关记录信息</td></tr>')
        }
        B.start = V.Page;
        B.count = V.Total;
        $("div#lpager").paginate(B);
    };
    $("div#list div.query button.query").click(function (C) {
        query($("div#list div.query form").serializeParam(), "QueryPay");
        return false;
    });
    query($("div#list div.query form").serializeParam(), "QueryPay");

    $("div#list div.query button.export").click(function () {
        var params = $("div#list div.query form").serializeParam();
        $.ajax({
            url: "../Export/QueryPay",
            dataType: "json",
            data: params,
            timeout: timeout,
            success: function (rep) {
                if (rep.Result) {
                    $.downloadFile('../Export/Download', 'post', rep.Message);
                } else {
                    alert(rep.Message);
                }
            }
        }).fail(function (T, S) {
            alert("浏览器超时未响应，请刷新重试");
        });
        return false;
    });
});